﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wizard.Source;

namespace Wizard.MsSql
{
    partial class Application : IWizardApplication, IOwnObjects
    {
        //private Wizard.Engine.IWizardEngine mEngine;

        internal Application()
        {
            //throw new Exception("Applications should be created from Engine only.");
        }

        //public Application(Wizard.Engine.IWizardEngine engine)
        //{
        //    mEngine = engine;
        //}

        #region IApplication Members

        //Wizard.Engine.IWizardEngine IApplication.Engine
        //{
        //    get { return mEngine; }
        //}

        IClassSource IWizardApplication.GetClassSource(string className)
        {
            var newClassSource = new ClassSource();
            newClassSource.Application = this;
            newClassSource.ClassName = className;
            return newClassSource;
        }

        #endregion

        #region IOwnObjects Members

        void IOwnObjects.DeleteOwnedObjects(DataStoreContext datastore)
        {
            datastore.DeleteObjects(ClassSources);            
        }

        int IWizardApplication.ApplicationId
        {
            get
            {
                return this.ApplicationId;
            }
        }

        string IWizardApplication.ApplicationName
        {
            get
            {
                return this.ApplicationName;
            }
        }

        ISourceFile[] IWizardApplication.GetSourceFiles()
        {
            var method1 = this.SourceFiles.ToArray();
            return method1;
        }

        ISourceFile IWizardApplication.NewSourceFile(Wizard.IDataStoreContext dataStoreContext, string sourceFileName)
        {
            var newSourceFile=new SourceFile();
            //this.SourceFile.Add(newSourceFile);
            newSourceFile.FileName = sourceFileName;
            newSourceFile.SourceText = string.Empty;
            newSourceFile.Application = this;
            dataStoreContext.SaveAllChanges();
            return newSourceFile;
        }

        void IWizardApplication.DeleteSourceFile(IDataStoreContext dataStoreContext, int sourceFileId)
        {
            dataStoreContext.DeleteSourceFile(sourceFileId);
        }

        #endregion

        Wizard.Runtime.IClassInstance IWizardApplication.Instantiate(IDataStoreContext dataStoreContext)
        {
            int appId = this.ApplicationId;
            foreach (ClassSource s in this.ClassSources)
            {
                if (s.ClassName == "Application")
                {
                    var x = new ClassInstance();
                    x.ClassSource = s;
                    
                    return x;
                }
            }
            throw new Exception(string.Format("The application {0} does not contain an Application class.", this.ApplicationId));
        }


        #region IWizardApplication Members


        void IWizardApplication.Update(IDataStoreContext dataStoreContext, string applicationName)
        {
            this._ApplicationName = applicationName;
            dataStoreContext.SaveAllChanges();
        }

        void IWizardApplication.DeleteClassSources(IDataStoreContext dataStoreContext)
        {
            dataStoreContext.DeleteClassSources(this);
            dataStoreContext.SaveAllChanges();
        }

        void IWizardApplication.UpdateClass(IDataStoreContext dataStoreContext, string applicationName)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
